package com.library.web.controller.app;

/**
 * @Author julyWhj
 * @Description $
 * @Date 2022/3/25 4:57 下午
 **/

import com.library.common.annotation.Log;
import com.library.common.core.controller.BaseController;
import com.library.common.core.domain.AjaxResult;
import com.library.common.core.page.TableDataInfo;
import com.library.common.enums.BusinessType;
import com.library.common.utils.poi.ExcelUtil;
import com.library.domain.AppActivity;
import com.library.framework.manager.BizCodeManager;
import com.library.service.IAppActivityService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 活动管理Controller
 *
 * @author ruoyi
 * @date 2022-03-25
 */
@RestController
@RequestMapping("/app/activity")
public class AppActivityController extends BaseController {
    @Autowired
    private IAppActivityService appActivityService;
    @Autowired
    private BizCodeManager bizCodeManager;

    /**
     * 查询活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('app:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppActivity appActivity) {
        startPage();
        List<AppActivity> list = appActivityService.selectAppActivityList(appActivity);
        return getDataTable(list);
    }

    /**
     * 获取活动列表
     *
     * @return
     */
    @GetMapping("/selectActivity")
    public AjaxResult selectActivity() {
        AppActivity appActivity = new AppActivity();
        appActivity.setActivityStatus(0L);
        List<AppActivity> appActivities = appActivityService.selectAppActivityList(appActivity);
        List<Map<String, String>> mapList = new ArrayList<>();
        appActivities.forEach(activity -> {
            Map<String, String> map = new HashMap<>();
            map.put("id", activity.getActivityId().toString());
            map.put("no", activity.getActivityNo());
            map.put("name", activity.getActivityTitle());
            mapList.add(map);
        });
        return AjaxResult.success(mapList);
    }

    /**
     * 导出活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('app:activity:export')")
    @Log(title = "活动管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppActivity appActivity) {
        List<AppActivity> list = appActivityService.selectAppActivityList(appActivity);
        ExcelUtil<AppActivity> util = new ExcelUtil<AppActivity>(AppActivity.class);
        util.exportExcel(response, list, "活动管理数据");
    }

    /**
     * 获取活动管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:activity:query')")
    @GetMapping(value = "/{activityId}")
    public AjaxResult getInfo(@PathVariable("activityId") Long activityId) {
        return AjaxResult.success(appActivityService.selectAppActivityByActivityId(activityId));
    }

    /**
     * 新增活动管理
     */
    @PreAuthorize("@ss.hasPermi('app:activity:add')")
    @Log(title = "活动管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppActivity appActivity) {
        appActivity.setActivityNo(bizCodeManager.createHd());
        return toAjax(appActivityService.insertAppActivity(appActivity));
    }

    /**
     * 修改活动管理
     */
    @PreAuthorize("@ss.hasPermi('app:activity:edit')")
    @Log(title = "活动管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppActivity appActivity) {
        return toAjax(appActivityService.updateAppActivity(appActivity));
    }

    /**
     * 删除活动管理
     */
    @PreAuthorize("@ss.hasPermi('app:activity:remove')")
    @Log(title = "活动管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{activityIds}")
    public AjaxResult remove(@PathVariable Long[] activityIds) {
        return toAjax(appActivityService.deleteAppActivityByActivityIds(activityIds));
    }

    /**
     * 活动报名接口
     *
     * @param activityNo 活动编号
     * @return
     */
    @Log(title = "活动报名", businessType = BusinessType.OTHER)
    @GetMapping("signUpActivity")
    public AjaxResult signUpActivity(String activityNo) {
        appActivityService.signUpActivity(activityNo);
        return AjaxResult.success();
    }

    /**
     * 扫码签到接口
     *
     * @param activityNo 活动编号
     * @return
     */
    @Log(title = "扫码签到", businessType = BusinessType.OTHER)
    @GetMapping("sweepTheCodeSign")
    public AjaxResult sweepTheCodeSign(String activityNo) {
        appActivityService.sweepTheCodeSign(activityNo);
        return AjaxResult.success();
    }
}

